﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LDAPOfficeTool
{
    public partial class Person2DisplayUniversal : UserControl
    {
        bool Initialized;  // if the initialization was well done
        string MainOrg;
        string MainOrgKurzBez;
        LDAPServiceNS.PersonBean PersBean;
        LDAPServiceNS.OrganisationBean[] OrgBeanArr;
        LDAPServiceNS.LdapGvAtQueryClient LdapClient;
        Microsoft.Office.Interop.Word.Document Doc2Handle;

        const int ContractedHeightLimit = 46;
        const int ExpandedHeightLimit = 129;
        const int MaxNameLengthLimit = 36;
        const int LblOrgTooltipMinLenLimit = 42;
 
        string GetPersonNameWithTitle()
        {
            string RetVal;
            RetVal = "";
            if (PersBean.personalTitle != null)
            {
                if (PersBean.personalTitle.Length > 0)
                {
                    RetVal = PersBean.personalTitle + " ";
                }
            }
            RetVal = RetVal + PersBean.givenName + " " + PersBean.sn;
            if (PersBean.gvIntTitle != null)
            {
                RetVal = RetVal + ", " + PersBean.gvIntTitle;
            }
            return RetVal;
        }


// 2011.04.14 Bobic
// mail from Pesendorfer, at Addresat passiert nix beim Einfügen -> actually, it was a catch
       string GetPersonPostalAddress()
       {
           string RetVal;
           RetVal = "";
           string [] AddressPartArr;
           if (PersBean.postalAddress != null)
           {
               if (PersBean.postalAddress.Length > 0)
               {
                   AddressPartArr = PersBean.postalAddress.Split('$');
                   RetVal = AddressPartArr[0] + ",";
                   for (int i = 1; i < AddressPartArr.Length; i++)
                   {
                       RetVal = RetVal + " " + AddressPartArr[i];
                   }
               }
               else
               {
                   RetVal = "-0-";
               }
           }
           else
           {
               RetVal = "- adresse nicht vorhanden -";
           }
           return RetVal;
               
       }


        string GetPersonOrg()
        {
            string RetVal;
            RetVal = "";
            bool gvCnExists;
            gvCnExists = false;
            if (PersBean != null)
            {
//                sRetVal = torgBean[0].ou + ", " + torgBean[0].cn[0];
                if (OrgBeanArr.Length > 1)
                {
                    RetVal = "> org ";
                }
// 2011.06.29 correction
                if (OrgBeanArr[0].gvCn != null)
                {
                    if (OrgBeanArr[0].gvCn.Length > 0)
                    {
                        RetVal = RetVal + OrgBeanArr[0].gvCn;
                        gvCnExists = true;
                    }
                }
                if (!gvCnExists)
                {
                    RetVal = RetVal + StringUtils.StringStorage2String(OrgBeanArr[0].cn);
                }
            }
            else
            {
                RetVal = "org ?";
            }
            return RetVal;
        }


        string GetPersonEMail()
        {
            string RetVal = "";
                if (PersBean.mail.Length > 0)
                {
                    RetVal = PersBean.mail.ToLower ();
                }
                else
                {
                    RetVal = "keine E-Mail Adresse";
                }
            return RetVal;
        }


        string GetPersonPhone()
        {
            string RetVal;
            RetVal = "";
            if (PersBean.telefoneNumber != null)
            {
                RetVal = StringUtils.StringTable2OneLine(PersBean.telefoneNumber, ',');
            }
            else
            {
                RetVal = "keine Telefonnummer";
            }
            return RetVal;
        }


        public Person2DisplayUniversal(LDAPServiceNS.PersonBean pPerson, string psMainOrg, string psMainOrgKurzBez, Microsoft.Office.Interop.Word.Document pdoc2Handle)
        {
            InitializeComponent();
            PersBean = pPerson;
            // correct person's email address to lower
            if (PersBean.mail != null)
            {
                PersBean.mail = PersBean.mail.ToLower();
            }
            else
            {
                PersBean.mail = "";
            }
            MainOrg = psMainOrg;
            MainOrgKurzBez = psMainOrgKurzBez;
            Initialized = false;
            linklblLDAP.Text = StringUtils.CutOffWithTail(GetPersonNameWithTitle(), "...", MaxNameLengthLimit);
            lblEMail.Text = GetPersonEMail ();
            lblPhone.Text = GetPersonPhone();
            lblMainOrg.Text = MainOrgKurzBez;
            this.Height = ContractedHeightLimit;
            Doc2Handle = pdoc2Handle;
            btnExpand.Visible = true;
            btnContract.Visible = false;
         }

        private void btnExpand_Click(object sender, EventArgs e)
        {
//            MessageBox.Show("Einfugen loc: " + btnEinfuegen.Location.ToString() + "  exp.heicht: " + ciExpandedHeight.ToString());
                if (!Initialized)
                {
                    LdapClient = new LDAPServiceNS.LdapGvAtQueryClient();
                    // BBB09
                    OrgBeanArr = LdapClient.findOrganizationsBy(null, PersBean.gvOu[0], null, null, null, null, null, false, true, 10);
// the old method took 9 arguments, removed on 2011.02.09
//                   torgBean = ldapClient.findOrganizationsBy(null, personBean.gvOu[0], null, null, null, null, false, true, 10);
                    lblOrg.Text = GetPersonOrg();
                    if (lblOrg.Text.Length > LblOrgTooltipMinLenLimit)
                    {
                        toolTipOrgLabel.SetToolTip(lblOrg, lblOrg.Text);
                        lblOrg.Text = GetPersonOrg().Substring(0, LblOrgTooltipMinLenLimit) + "...";
                    }
                    Initialized = true;
                    LdapClient.Close();
                }
            this.Height = ExpandedHeightLimit;
            btnExpand.Visible = false;
            btnContract.Visible = true;
            btnEinfuegen.Visible = true;
//            MessageBox.Show("Einfugen loc: " + btnEinfuegen.Location.ToString() + "  exp.heicht: " + ciExpandedHeight.ToString());
         }



        private void btnContract_Click(object sender, EventArgs e)
        {
            btnExpand.Visible = true;
            btnContract.Visible = false;
            this.Height = ContractedHeightLimit;
            btnEinfuegen.Visible = false;
        }



        private void linklblLDAP_MouseClick(object sender, MouseEventArgs e)
        {
            BrowserForm Person2LDAP;
            Person2LDAP = new BrowserForm();
            string Link2Open;
            Link2Open = "http://ldap.gv.at/lfrz.at/ldapw/index.php?sPage=1&showDetailDn=" + PersBean.dn + "&detailType=person";
            Person2LDAP.SetUri(Link2Open);
            Person2LDAP.Visible = true;
        }


        private void lblEMail_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("mailto:" + PersBean.mail);
        }


        #region BLOCK_EINFUEGEN
        private void btnEinfuegen_Click(object sender, EventArgs e)
        {
            Point AuxPoint;
            AuxPoint = new Point(5, 5);
            contextMenuStripPActions.Show(btnEinfuegen, AuxPoint);
        }


        private void nameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Word.Selection Selection;
            Selection = Doc2Handle.Application.Selection;
            Selection.TypeText(GetPersonNameWithTitle() + " ");
        }


        private void telefonnummerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Word.Selection Selection;
            if (PersBean.telefoneNumber != null)
            {
                Selection = Doc2Handle.Application.Selection;
                Selection.TypeText(GetPersonPhone());
            }
            else
            {
                MessageBox.Show("Keine Telefonnummer");
            }
        }


        // called by Einfügen -> Adressat
        private void adressatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string Text2Insert;
            try
            {

                Text2Insert = MainOrg + Environment.NewLine;
                Text2Insert = Text2Insert + "z.H. " + GetPersonNameWithTitle() + Environment.NewLine;
// 2011.06.29 gerold correction
//                Text2Insert = Text2Insert + OrgBeanArr[0].cn[0] + Environment.NewLine;
                Text2Insert = Text2Insert + GetPersonOrg() + Environment.NewLine;
                Text2Insert = Text2Insert + GetPersonPostalAddress() + Environment.NewLine;

                if (PersBean.telefoneNumber != null)
                {
                    Text2Insert = Text2Insert + "Tel: " + GetPersonPhone() + Environment.NewLine;
                }
                Text2Insert = Text2Insert + PersBean.mail + Environment.NewLine;
                //            person2Display.g
                Microsoft.Office.Interop.Word.Selection Selection;
                Selection = Doc2Handle.Application.Selection;
                Selection.TypeText(Text2Insert);
            }
            catch (Exception exAddresat)
            {
                MessageBox.Show("Exception at Adressat einfügen: " + Environment.NewLine + exAddresat.Message);
            }
        }


        private void AnschriftToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string Text2Enter;
            string[] AddressPartsArr;
            Microsoft.Office.Interop.Word.Selection Selection;
            if (PersBean.postalAddress != null)
            {
                AddressPartsArr = PersBean.postalAddress.Split('$');
                Text2Enter = GetPersonNameWithTitle() + Environment.NewLine;
                Text2Enter = Text2Enter + GetPersonPostalAddress();
                Selection = Doc2Handle.Application.Selection;
                Selection.TypeText(Text2Enter);
            }
            else
            {
                MessageBox.Show("Adresse ist nicht verfügbar");
            }
        }
        #endregion
    }
}
